unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TMainForm = class(TForm)
    Edit: TEdit;
    Memo: TMemo;
    AddButton: TButton;
    s1: TShape;
    s4: TShape;
    s7: TShape;
    s6: TShape;
    s2: TShape;
    s5: TShape;
    s3: TShape;
    procedure AddButtonClick(Sender: TObject);
    procedure s1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

function IntToBin(Value: integer; Digits: integer): string;
var
  i: integer;
begin
  result := '';
  for i := 0 to Digits - 1 do begin
    if Value and (1 shl i) > 0
    then result := '1' + result
    else result := '0' + result;
  end;
end;

procedure TMainForm.AddButtonClick(Sender: TObject);
begin
  memo.Lines.Add(edit.Text)
end;

procedure TMainForm.s1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  function b2i(aShape: TShape): integer;
  begin
    result := ord(aShape.Brush.Color = clBlack);
  end;
var
  lShape: TShape;
  lb: byte;
begin
  lShape := (Sender as TShape);
  if lShape.Brush.Color = clBlack then begin
    lShape.Brush.Color := clWhite
  end
  else begin
    lShape.Brush.Color := clBlack;
  end;
  lb := b2i(s1) +
        b2i(s2) shl 1 +
        b2i(s3) shl 2 +
        b2i(s4) shl 3 +
        b2i(s5) shl 4 +
        b2i(s6) shl 5 +
        b2i(s7) shl 6;
  edit.Text := IntToBin(lb shl 1, 8);
end;

end.
